﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Granite.DataAnnotations;
using System.ComponentModel.DataAnnotations;

namespace Granite.Modeling.Tests.DataAnnotations
{
	[TestClass]
	public class MaxLengthAttributeTests
	{

		[TestMethod]
		public void MaxLengthAttribute_NullContextTest()
		{
			var result = new MaxLengthAttribute(10);
			try
			{
				result.GetValidationResult(null, (ValidationContext)null);
				Assert.Fail("Excepted an ArgumentNullException");
			}
			catch (ArgumentNullException ex)
			{
				Assert.AreEqual("validationContext", ex.ParamName);
			}
		}


		[TestMethod]
		public void MaxLengthAttribute_NullTest()
		{
			var result = new MaxLengthAttribute(10);
			Assert.AreEqual(ValidationResult.Success, result.GetValidationResult(null, new ValidationContext("Age", null, null)));
		}

		[TestMethod]
		public void MaxLengthAttribute_ConstructorTest()
		{
			try
			{
				var result = new MaxLengthAttribute(0);
				Assert.Fail("Excepted an ArgumentNullException");
			}
			catch (ArgumentException ex)
			{
				Assert.AreEqual("maxLength", ex.ParamName);
			}
		}

		[TestMethod]
		public void MaxLengthAttribute_MaxLengthTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreEqual(5, result.MaxLength);
		}

		[TestMethod]
		public void MaxLengthAttribute_StringEmptyTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreEqual(ValidationResult.Success, result.GetValidationResult("", new ValidationContext("Age", null, null)));
		}

		[TestMethod]
		public void MaxLengthAttribute_StringLowTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreEqual(ValidationResult.Success, result.GetValidationResult("1234", new ValidationContext("Age", null, null)));
		}

		[TestMethod]
		public void MaxLengthAttribute_StringEqualTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreEqual(ValidationResult.Success, result.GetValidationResult("12345", new ValidationContext("Age", null, null)));
		}

		[TestMethod]
		public void MaxLengthAttribute_StringHighTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreNotEqual(ValidationResult.Success, result.GetValidationResult("123456", new ValidationContext("Age", null, null)));
		}

		[TestMethod]
		public void MaxLengthAttribute_ListLowTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreEqual(ValidationResult.Success, result.GetValidationResult(MakeList(4), new ValidationContext("Age", null, null)));
		}

		[TestMethod]
		public void MaxLengthAttribute_ListEqualTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreEqual(ValidationResult.Success, result.GetValidationResult(MakeList(5), new ValidationContext("Age", null, null)));
		}

		[TestMethod]
		public void MaxLengthAttribute_ListHighTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreNotEqual(ValidationResult.Success, result.GetValidationResult(MakeList(6), new ValidationContext("Age", null, null)));
		}

		List<int> MakeList(int length)
		{
			var result = new List<int>();
			for (int i = 0; i < length; i++)
			{
				result.Add(i);
			}
			return result;
		}

		[TestMethod]
		public void MaxLengthAttribute_EnumerationLowTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreEqual(ValidationResult.Success, result.GetValidationResult(MakeEnumeration(4), new ValidationContext("Age", null, null)));
		}

		[TestMethod]
		public void MaxLengthAttribute_EnumerationEqualTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreEqual(ValidationResult.Success, result.GetValidationResult(MakeEnumeration(5), new ValidationContext("Age", null, null)));
		}

		[TestMethod]
		public void MaxLengthAttribute_EnumerationHighTest()
		{
			var result = new MaxLengthAttribute(5);
			Assert.AreNotEqual(ValidationResult.Success, result.GetValidationResult(MakeEnumeration(6), new ValidationContext("Age", null, null)));
		}

		IEnumerable<int> MakeEnumeration(int length)
		{
			var value = 0;
			while (value < length)
			{
				yield return value;
				value++;
			}
		}

		[TestMethod]
		public void MaxLengthAttribute_TypeTest()
		{
			var result = new MaxLengthAttribute(10);
			try
			{
				result.GetValidationResult(new Object(), new ValidationContext("Age", null, null));
				Assert.Fail("Excepted an ArgumentNullException");
			}
			catch (ArgumentException ex)
			{
				Assert.AreEqual("value", ex.ParamName);
			}
		}

	}
}







